package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.v2.FeedbackPO;
import com.k2data.k2app.mapper.v2.FeedbackMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.rest.SysClient;
import com.k2data.k2app.rest.domain.RectList;
import com.k2data.k2app.service.v2.FeedbackService;
import com.k2data.k2app.utils.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cuilibo@k2data.com.cn
 * @date  2018年05月22日10:19:37
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private SysClient sysClient;

    @Autowired
    public FeedbackController () {
    }

    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "FeedbackPO", value = "FeedbackPO", required = true, dataType = "FeedbackPO", paramType = "body")
    })
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> post(@RequestBody FeedbackPO event) throws Exception {
        return ResponseHelper.success(feedbackService.insert(event));
    }

    @ApiOperation(value = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "FeedbackPO", value = "FeedbackPO", required = true, dataType = "FeedbackPO", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putEvent(@PathVariable("id") Long id,
                                                         @RequestBody FeedbackPO event) throws Exception {
        event.setId(id);
        return ResponseHelper.success(feedbackService.update(event));
    }

    @ApiOperation(value = "列表")
    @GetMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "当前登陆用户id(userId)", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "item (desc)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "title", dataType = "String", paramType = "query"),
    })
    public ResponseEntity<CommonResultResponse> getList(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "customerId", required = false) String customerId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "title", required = false)String title) throws Exception {
        FeedbackPO cond = new FeedbackPO();
        if (!StringUtils.isEmpty(customerId)){
            cond.setCustomerId(Long.parseLong(customerId));
        }
        if (userId != null){
            // 判断用户是否是系统管理员
            if(cond.getUserId()!=null){
                RectList rolesByUser = sysClient.getRolesByUser(cond.getUserId());
                List list = rolesByUser.getResult();
                if(list!=null && list.size()>0){
                    for(int i=0;i<list.size();i++){
                        Map map = (Map)list.get(i);
                        if(map.get("enname")!=null && !map.get("enname").toString().equals("Administrator")){
                        }else{
                            cond.setUserId(null);
                        }
                    }
                }
            }
        }
        if (!StringUtils.isEmpty(order)) {
            cond.setOrderBy(order+" DESC");
        }

        cond.setProblemTitle(title);
        int pageNumReal = pageNum == null ? 1 : pageNum;
        int pageSizeReal = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;

        int i = feedbackMapper.selectCount(cond);
        PageHelper.startPage(pageNumReal, pageSizeReal);
        PageInfo pageInfo = new PageInfo(pageNumReal, pageSizeReal, i);

        cond.setPageNum(pageNumReal);
        cond.setPageSize(pageSizeReal);
        List<FeedbackPO> source = feedbackService.list(cond);
        return ResponseHelper.success(source,pageInfo);
    }

}
